package cn.op.portal.towel.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import cn.op.portal.category.domain.Category;
import cn.op.portal.category.service.CategoryService;
import cn.op.portal.common.action.BaseAction;
import cn.op.portal.common.vo.Page;
import cn.op.portal.towel.domain.Towel;
import cn.op.portal.towel.service.TowelService;
import cn.op.portal.utils.ImgThumUtil;
import cn.op.portal.utils.Md5Utils;

public class TowelAction extends BaseAction implements ModelDriven<Towel>, Preparable{
	private Integer id;
	private Integer categoryId;
	private File imageFile;
	private String imageFileFileName;
	private String saveFile;
	private Towel towel;
	private Page<Towel> page;
	private int imgIndex;	//点击的是第几张图片
	
	@Resource
	private TowelService towelService;
	@Resource
	private CategoryService categoryService;
	
	public String addUI() {
		List<Category> categorys = categoryService.getAll();
		getRequest().setAttribute("categorys", categorys);
		return "addUI";
	}
	
	public String updateUI() {
		List<Category> categorys = categoryService.getAll();
		getRequest().setAttribute("categorys", categorys);
		return "updateUI";
	}
	
	public String add(){		
		String extention = imageFileFileName.substring(imageFileFileName.lastIndexOf("."));
		imageFileFileName = imageFileFileName.substring(0, imageFileFileName.lastIndexOf("."));
//		imageFileFileName = Md5Utils.doMd5(imageFileFileName) + extention;
		imageFileFileName = String.valueOf(Calendar.getInstance().getTimeInMillis()) + extention;	//当前时间做文件名
		String imageFoder = ServletActionContext.getServletContext().getRealPath("/images") + "/";
		saveFile = imageFoder + imageFileFileName;
		towel.setImage(imageFileFileName);
		try {
			saveImage(saveFile);	//保存图片
			String imgdist = imageFoder + "thum_" + imageFileFileName;	//原始图片文件名前加”thum_“作为缩略图文件名，这样数据库中就不用存缩略图字段，页面中访问时获取原始图片名前加”thum_“就是缩略图路径
			ImgThumUtil.reduceImg(saveFile, imgdist, 90, 90);	//生成缩略图
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		towelService.add(towel);
		return SUCCESS;
	}
	
	public String deleteById(){
		towelService.deleteById(id);
		return null;
	}
	
	public String update() {
		//TODO 修改图片未实现
		towelService.update(towel);
		return SUCCESS;
	}

	public String findById(){
		towel = towelService.findById(id);
		return SUCCESS;
	}
	
	public String findPage() throws Exception{
		page = towelService.findPage(pageNum,pageSize);
		List<Category> categorys = categoryService.getAll();
		getRequest().setAttribute("categorys", categorys);
		return "towelList";
	}
	
	public String imgShowData() throws Exception{
		if(categoryId==-1){			//所有类别
			page = towelService.findPage(pageNum,pageSize);
			return "imgShowData";
		}else{
			page = towelService.findPageByCategory(pageNum,pageSize,categoryId);
			return "imgShowData";
		}
	}
	
	public String findPageByCategory() throws Exception{
		List<Category> categorys = categoryService.getAll();
		getRequest().setAttribute("categorys", categorys);
		if(categoryId==-1){			//所有类别
			page = towelService.findPage(pageNum,pageSize);
			return "towelList";
		}else{
			page = towelService.findPageByCategory(pageNum,pageSize,categoryId);
			return "towelList";
		}
	}
	

	@Override
	public Towel getModel() {
		return towel;
	}

	@Override
	public void prepare() throws Exception {
		if(id == null) {
			towel = new Towel();
		} else {
			towel = towelService.findById(id);
		}		
	}
	
	
	private void saveImage(String file) throws IOException {
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(imageFile));
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
		try {
			int ch;
			while((ch=bis.read())!=-1){
				bos.write(ch);
			}
		}finally {
			if(bis != null){
				bis.close();
			}
			if(bos != null){
				bos.close();
			}
		}
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Towel getTowel() {
		return towel;
	}

	public void setTowel(Towel towel) {
		this.towel = towel;
	}

	public File getImageFile() {
		return imageFile;
	}

	public void setImageFile(File imageFile) {
		this.imageFile = imageFile;
	}

	public String getImageFileFileName() {
		return imageFileFileName;
	}

	public void setImageFileFileName(String imageFileFileName) {
		this.imageFileFileName = imageFileFileName;
	}

	public void setSaveFile(String saveFile) {
		this.saveFile = saveFile;
	}

	public Integer getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(Integer categoryId) {
		this.categoryId = categoryId;
	}

	public String getSaveFile() {
		return saveFile;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public int getImgIndex() {
		return imgIndex;
	}

	public void setImgIndex(int imgIndex) {
		this.imgIndex = imgIndex;
	}


}
